package com.ziq.zcamp.place

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.zcamp.R
import com.ziq.zcamp.api.respond.camp.CampConditionTree
import com.ziq.zcamp.bean.place.CampListConditionCategory
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.databinding.ItemCampListConditionCategoryBinding
import com.ziq.zcamp.util.ImageUtil

class CampListConditionCategoryAdapter : RecyclerView.Adapter<BindingViewHolder<ItemCampListConditionCategoryBinding>>(){

    init {
        setHasStableIds(true)
    }

    var dataList: List<CampConditionTree>? = null

    var onClickListener: ((position:Int, category:CampConditionTree)->Unit)? = null

    var inFilterPanel = false
    var filterPanelPosition = -1

    fun setData(list: List<CampConditionTree>?){
        this.dataList = list
        notifyDataSetChanged()
    }

    private var selectedList : MutableMap<Int, MutableList<Int>>? = null
    fun updateSelectedCondition(selectedConditionList : MutableMap<Int, MutableList<Int>>?){
        this.selectedList = selectedConditionList
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return dataList?.size?:0
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemCampListConditionCategoryBinding> {
        return createBindingViewHolder(parent)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemCampListConditionCategoryBinding>, position: Int) {
        val item = dataList?.get(position)
        item?.let {
            holder.binding.tvName.text = item.name
            var imageList: List<ImageInfo> =  mutableListOf<ImageInfo>()
            if (item.pictures?.isNotEmpty() == true){
                item.pictures?.let {
                    imageList = GsonUtils.fromJson(it, GsonUtils.getListType(ImageInfo::class.java))
                }
            }
            if(imageList.isNotEmpty()){
                ImageUtil.updateImageFromNetwork(holder.binding.ivIcon, imageList[0].url)
            }

            holder.itemView.click {
                onClickListener?.invoke(position, item)
            }

            if(inFilterPanel){
                holder.binding.llItem.background = null
                if(filterPanelPosition == position){
                    holder.binding.ivStatus.setImageResource(R.drawable.icon_camp_list_filter_panel_status_sel)
                } else {
                    holder.binding.ivStatus.setImageResource(R.drawable.icon_camp_list_filter_panel_status_nor)
                }
            } else {
                holder.binding.llItem.setBackgroundResource(R.drawable.bg_f6f6f6_8dp)
                var hasSelected = false
                item.children?.let {
                    for (cateroty in it){
                        if(selectedList?.containsKey(cateroty.id?:-1) == true){
                            if(selectedList?.get(cateroty?.id?:-1)?.isNotEmpty() == true){
                                hasSelected = true
                                break
                            }
                        }
                    }
                }

                if(hasSelected){
                    holder.binding.ivStatus.setImageResource(R.drawable.icon_camp_list_filter_status_sel)
                } else {
                    holder.binding.ivStatus.setImageResource(R.drawable.icon_camp_list_filter_status_nor)
                }
            }

        }
    }

}